<template>
  <div class="playlist-container">
    <div class="top-wrap" v-if="playlist.id">
      <div class="img-wrap">
        <!-- 封面 -->
        <img :src="$utils.genImgUrl(playlist.coverImgUrl, 200)" />
      </div>

      <div class="info-wrap">
        <!-- 名字 -->
        <div class="title-wrap">
          <p class="title">{{ playlist.name }}</p>
        </div>
        <div class="creator-wrap">
          <!-- 头像 -->
          <!-- 神经病VUE,不加v-if="playlist.creator !== undefined" 报错 -->
          <img
            class="avatar"
            :src="$utils.genImgUrl(playlist.creator.avatarUrl, 80)"
            v-if="playlist.creator !== undefined"
          />
          <!-- 名字 -->
          <p class="creator" v-if="playlist.creator !== undefined">{{ playlist.creator.nickname }}</p>
          <!-- 时间 -->
          <p class="create-time">{{ $utils.formatDate(playlist.createTime, 'yyyy-MM-dd') }} 创建</p>
        </div>

        <div class="action-wrap">
          <div class="n-button button">
            <Icon class="icon middle" color="white" type="play-round" />
            <span class="middle" @click="toPlayAll">播放全部</span>
          </div>
        </div>

        <div class="desc-wrap">
          <p class="desc" v-if="tagsText">
            <span>标签：{{ tagsText }}</span>
          </p>
          <p class="desc" v-if="playlist.description">
            <span class="value">简介：{{ playlist.description }}</span>
          </p>
        </div>
      </div>
    </div>

    <div class="tabs-wrap">
      <el-tabs v-model="activeIndex">
        <!-- 歌曲 -->
        <el-tab-pane label="歌曲列表" name="1">
          <SongCard :songs="songs" />
        </el-tab-pane>

        <el-tab-pane :label="`评论(${total})`" name="2">
          <Comment :id="id" @update="onCommentsUpdate" type="playlist" />
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { getPlaylistDetail, getSongDetail } from 'network/request'
import { mapState, mapMutations, mapActions } from '@/store/helper/music'
export default {
  name: 'playlist',
  metaInfo() {
    return {
      title: this.playlist.name ? this.playlist.name : '我永远喜欢宫胁咲良',
    }
  },
  data() {
    return {
      activeIndex: '1',
      // 总条数
      total: 0,
      playlist: {},
      songs: [],
    }
  },
  methods: {
    // 获取歌单详情
    async _getPlaylistDetail() {
      const { playlist } = await getPlaylistDetail({
        id: this.id,
        n: 100000,
      })
      this.avatarUrl = playlist.creator.avatarUrl
      this.nickname = playlist.creator.nickname
      this.playlist = playlist
      // 获取歌单详情
      const newSongs = await this._getSongDetail(playlist.trackIds.map((x) => x.id).slice(0, 500))
      this.songs = newSongs
    },
    // 获取歌单详情
    async _getSongDetail(trackIds) {
      let params = this.$utils.songDetailParams(trackIds)
      const { songs } = await getSongDetail(params)
      return songs.map((x) => this.$utils.createSong(x))
    },
    toPlayAll() {
      this.startSong(this.songs[0])
      this.setPlaylist(this.songs)
    },
    onCommentsUpdate(total) {
      this.total = total
    },
    scrollToHeader() {
      let topWrap = document.getElementsByClassName('playlist-container')[0]
      topWrap.scrollIntoView({ behavior: 'smooth' })
    },
    ...mapMutations(['setPlaylist']),
    ...mapActions(['startSong']),
  },
  computed: {
    id() {
      return this.$route.params.id
    },
    tagsText() {
      if (this.playlist && this.playlist.tags) {
        return this.playlist.tags.join('/')
      }
    },
  },
  watch: {
    id: {
      handler(newId) {
        if (newId) {
          this._getPlaylistDetail()
          this.$nextTick(() => {
            this.scrollToHeader()
          })
        }
      },
      immediate: true,
    },
  },
}
</script>

<style lang="scss">
.el-tabs__nav-wrap::after {
  background-color: var(--border) !important;
}
.el-tabs__active-bar {
  background-color: $theme-color;
}

.el-tabs__item:hover,
.el-tabs__item.is-active {
  color: $theme-color;
}
</style>
<style lang="scss" scoped>
.top-wrap {
  display: flex;
  padding: 36px;

  .img-wrap {
    width: 200px;
    height: 200px;
    margin-right: 24px;

    img {
      width: 100%;
      height: 100%;
    }
  }

  .info-wrap {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;

    .title-wrap {
      display: flex;
      align-items: center;
      margin-bottom: 12px;

      .title {
        font-size: $font-size-lg;
        color: var(--font-color-white);
      }
    }

    .action-wrap {
      margin-bottom: 18px;

      .n-button {
        display: inline-block;
        padding: 5px 16px;
        border: 1px solid var(--button-border-color);
        border-radius: 2px;
        text-align: center;
        cursor: pointer;
      }

      .button {
        background: #f95043;
        background: linear-gradient(to right, #fa5143, #f44d41, #d53b32);
        color: #fbdfdd;
        border: none;

        .icon {
          margin-right: 4px;
        }

        .middle {
          vertical-align: middle;
        }
      }
    }

    .creator-wrap {
      display: flex;
      align-items: center;
      margin-bottom: 18px;

      .avatar {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        margin-right: 8px;
      }

      .creator {
        margin-right: 8px;
      }

      .create-time {
        font-size: $font-size-sm;
      }
    }

    .desc {
      margin-bottom: 8px;

      .label {
        display: inline-block;
        margin-right: 8px;
      }

      .value {
        @include text-ellipsis-multi(3);
      }
    }
  }
}
.tabs-wrap {
  margin: 0 36px;
  .song-card {
    margin-left: -14px;
  }
}
</style>

